//
//  TodoController.m
//  table test
//
//  Created by tao故乡的云 on 2017/4/10.
//  Copyright © 2017年 故乡的云. All rights reserved.
//

#import "TodoController.h"
#import "ItemCell.h"

@interface TodoController ()<ItemCellDelegate>
@property(nonatomic,strong) NSMutableArray *items1;
@property(nonatomic,strong) NSMutableArray *items2;
@property(nonatomic,strong) ItemCell *previousCell;
@end

@implementation TodoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 44, 0);
    
}



#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.items1.count;
    }
    return self.items2.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"todoCell"];
    
    if(cell == nil){
        cell = [[ItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"todoCell"];
        cell.delegate = self;
    }
    cell.titleLabel.text = _items1[indexPath.row];
    return cell;
}

#pragma mark - table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
//    ItemCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    cell.selected = NO;
}

#warning BUG。。。
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
//    ItemCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    if (self.previousCell) {
//        if (![self.previousCell isEqual:cell]) {
//            [self.previousCell whenDidTouchEndEditOfPreviousCell];
//            self.previousCell = nil;
//        }
//    }
}

#pragma mark - proterties
- (void)itemCell:(ItemCell *)cell didLongPress:(UILongPressGestureRecognizer *)press {
    
}

- (void)itemCell:(ItemCell *)cell didDeleteBtnClick:(UIButton *)btn {
    
}

- (void)itemCell:(ItemCell *)cell didOn:(BOOL)on {
    
}

- (void)itemCell:(ItemCell *)cell didShowDelete:(BOOL)show {
    if (show) {
        self.previousCell = cell;
    }else {
        self.previousCell = nil;
    }
}

- (void)itemCellWillTouch:(ItemCell *)cell {
    if (self.previousCell) {
        [self.previousCell whenDidTouchEndEditOfPreviousCell];
        self.previousCell = nil;
    }
}

#pragma mark - proterties

- (NSMutableArray *)items1 {
    if (_items1 == nil) {
        _items1 = [NSMutableArray array];
        [_items1 addObjectsFromArray:@[@"技巧1：<< 向左滑动删除事项",@"技巧2：>> 向右滑动完成事项",@"技巧3：按住，调整待办位置"]];
    }
    
    return _items1;
}

- (NSMutableArray *)items2 {
    if (_items2 == nil) {
        _items2 = [NSMutableArray array];
    }
    
    return _items2;
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

@end
